<?php

/**
 * Created by PhpStorm.
 * Date: 2019/6/11
 * Time: 16:20
 */

namespace Lib\Paas;

use Lib\Base\Validate;

class Email extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 发送邮件
     *
     * @param array = [
     *      'receiver' => [] 【必填】接收人,
     *      'cc' => []       【必填】抄送人,
     *      'messageKey' => '【必填】文案模板ID',
     *      'verifyCode' => '【根据情况确认是否需要传】验证信息（以下字段都是模板替换）'
     *      'userName' => '【根据情况确认是否需要传】用户名称'
     * ]
     *
     * @return bool true-发送成功，false-发送失败
     *
     */
    public function sendEmail($emailData)
    {
        if (!isset($emailData['receiver']) || empty($emailData['receiver'])) {
            recordLog('邮件发送接口：邮件接收人不能为空！', __FILE__, __LINE__, LP_ERROR);
            return false;
        }
        if (is_string($emailData['receiver'])) {
            $emailData['receiver'] = array($emailData['receiver']);
        }
        foreach ($emailData['receiver'] as $email) {
            if (!Validate::checkEmail($email)) {
                recordLog('邮件发送接口：收件人:' . $email . '格式错误！', __FILE__, __LINE__, LP_ERROR);
                return false;
            }
        }
        if (isset($emailData['cc'])) {
            if (is_string($emailData['cc'])) {
                $emailData['cc'] = array($emailData['cc']);
            }
            foreach ($emailData['cc'] as $email) {
                if (!Validate::checkEmail($email)) {
                    recordLog('邮件发送接口：抄送人:' . $email . '格式错误！', __FILE__, __LINE__, LP_ERROR);
                    return false;
                }
            }
        }

        if (!isset($emailData['messageKey']) || !Validate::checkNaturalNum($emailData['messageKey'])) {
            recordLog('邮件发送接口：邮件模板ID错误，id=' . var_export($emailData['messageKey'], true), __FILE__, __LINE__, LP_ERROR);
            return false;
        }
        if (isset($emailData['verifyCode']) && !is_string($emailData['verifyCode'])) {
            recordLog('邮件发送接口：验证码必须为一个字符串，verifyCode=' . var_export($emailData['verifyCode'], true), __FILE__, __LINE__, LP_ERROR);
            return false;
        }
        if (isset($emailData['userName']) && !is_string($emailData['userName'])) {
            recordLog('邮件发送接口：用户名必须为一个字符串，userName=' . var_export($emailData['userName'], true), __FILE__, __LINE__, LP_ERROR);
            return false;
        }

        recordLog('邮件发送接口：请求数据data=' . var_export($emailData, true), __FILE__, __LINE__, LP_ERROR);

        //url
        $url = $this->getPaasRequestUrl('resource.email');
        $res = $this->http->post($url, http_build_query($emailData));
        recordLog('邮件发送接口：返回数据data=' . var_export($res, true), __FILE__, __LINE__, LP_ERROR);
        if ($res['ret'] != 0) {
            $this->tglogReport('框架请求ULink-Paas出现异常，curl_errno：' . $res['ret'] . '，curl_error：' . $res['msg'], 'resource.email');
            return false;
        }
        $res = $res['data'];
        //success
        if ($res['ret'] == '0' && $res['data']['ret'] == '0') {
            return true;
        } else {
            return false;
        }
    }
}
